<?php
namespace app\home\controller;
use think\Controller;
use think\Loader;
use think\request;
use think\Session;
use think\Cookie;
use think\Db;
/*
 * 
 */ 
class Mcenter extends Common
{
	public function _initialize()
    {
        parent::_initialize();
		$user = db('users')->where('id', session('home_uid'))->find();
		$this->assign('user', $user);
    }	

    public function index()
	{
		
		return view();
	}
	
	
	public function mychange()
	{
		return view();
	}
	
	public function money()
	{
		$fos_count = db('buy_list')->where(['m_id'=>session('home_uid'), 'status'=>2])->sum("count");
		$this->assign("all_amount", $fos_count);
		
		$fos_count = db('buy_list')->where(['m_id'=>session('home_uid'), 'status'=>2, 'is_end'=>0])->sum("count");
		$this->assign("amount", $fos_count);
		return view();
	}
	
	public function level()
	{
		return view();
	}
	
	public function signin(){
		if (request()->isPost()) {
			$ret = ['error'=>0, 'msg'=>""];
			$map['m_id'] = session("home_uid");
			$map['add_time'] = date("Y-m-d");
			$if_exist = db('signlog')->WHERE('add_time LIKE "'.date("Y-m-d").'%"')->find();
			if($if_exist){
				$ret['error'] = 1;
				$ret['msg'] = '今日已签到!';
			}else{
				$map['add_time'] = date("Y-m-d H:i:s");
				$tmp = db("signlog")->insert($map);
				if($tmp == false){
					$ret['error'] = 1;
				    $ret['msg'] = '签到失败，请稍后重试!';	
				}else{
					//TODO:添加到会员的TCSN钱包
					db('users')->where('id', session('home_uid'))->setInc("tcsn",1);
				}
			}
			echo json_encode($ret);
			die;
		}
		$key = date("Y-m");
		$rows = db('signlog')->WHERE('add_time LIKE "'.$key.'%"')->select();
		$signList = [];
		foreach($rows as $itm){
			$signList[] = ['signDay'=>date("d", strtotime($itm['add_time']))];
		}
		$this->assign("signList", json_encode($signList));
		$this->assign("curMonth", date("Y年m月"));
		return view();
	}
	
	public function task()
	{
		return view();
	}
	public function challenge()
	{
		return view();
	}
	public function knowledges()
	{
		return view();
	}
	
	public function stockbuy()
	{
		
		return view();
	}

	public function wallet()
	{
		return view();
	}
	
	public function asset_handling()
	{
		$type=input('param.type');
		$this->assign('type', $type);
		return view();
	}
	
	public function recharge()
	{
		return view();
	}
	
	public function withdraw()
	{
		$rows = db('cards')->where("m_id", session("home_uid"))->select();
		$this->assign('cards', $rows);
		return view();
	}
	
	public function address()
	{
		$rows = db('cards')->where("m_id", session("home_uid"))->select();
		$this->assign('cards', $rows);
		return view();
	}
	
	public function bill()
	{
		$type = input('param.type');
		if($type == 'fos'){
			$row = db("buy_list")->where('m_id', session('home_uid'))->order('id DESC')->select();
			$this->assign('list', $row);
		}
		
		if($type == 'f'){
			$row = db("withdraw")->where('m_id', session('home_uid'))->order('id DESC')->select();
			$this->assign('list', $row);
		}
		$this->assign('type', $type);
		return view();
	}

	public function add_alipay()
	{
		if (request()->isPost()) {
			$data = input('param.');
			$data['m_id'] = session('home_uid');
			$data['type'] = 1;
			$ret = db("cards")->insert($data);
			if($ret != false){
				echo 1;
			}else{
				echo 0;
			}
			die;
		}
		return view();
	}
	
	public function add_to()
	{
		if (request()->isPost()) {
			$data = input('param.');
			$data['m_id'] = session('home_uid');
			$ret = db("cards")->insert($data);
			if($ret != false){
				echo 1;
			}else{
				echo 0;
			}
			die;
		}
		return view();
	}
	
	public function edit_col(){
		$col = input('param.col');
		$val = input('param.val');
		$map[$col] = $val;
		$ret = db("users")->where('id', session('home_uid'))->update($map);
		if($ret){
			echo 0;
		}else{
			echo -1;
		}
	}
	
	
    public function  apply_withdraw(){
		$bank_id = input('post.bank_id');
		$pwd = input('post.pwd');
		$count = input('post.count');
		
		$data = ['error'=>0];
		//判断密码
		$user = db('users')->where('id', session('home_uid'))->find();
		if(md5($pwd) != $user['pwd']){
			$data['error'] = 1;
			$data['msg'] = "密码不正确";
		}else{
			if(($user['money']) < $count){
				$data['error'] = 1;
			    $data['msg'] = "没有足够的币";
			}else{
				Db::startTrans();
				try{
					db('users')->where('id', session('home_uid'))->setDec("money", $count);
					$bank = db('cards')->where('id', $bank_id)->find();
					$map['m_id'] = session('home_uid');
					$map['count'] = $count;
					$map['add_time'] = date("Y-m-d H:i:s");
					$map['bank'] = $bank['title'];
					$map['bank_no'] = $bank['no'];
					$map['bank_address'] = $bank['address'];
					$map['bank_username'] = $bank['username'];
					$ret = db('withdraw')->insert($map);
					Db::commit();
				}catch(\Exception $e){
					$data['error'] = 1;
			        $data['msg'] = "申请失败，请稍后重试!";
					Db::rollback();
				}
				
			}
		}
		echo json_encode($data);
	}
}

?>